package com.erp.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.erp.po.Categorys;
import com.erp.service.CategorysServiceImpl;
import com.erp.util.BootStrapPageHelper;
import com.erp.util.Page;
import com.google.gson.Gson;

@WebServlet("/admin/categorys/CategoryServlet")
public class CategoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private CategorysServiceImpl cates;
	public CategoryServlet() {
		cates = new CategorysServiceImpl();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		// 处理中文乱码
		/*request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");*/

		String op = request.getParameter("op");

		System.out.println("op: " + op + " ------- CategoryServlet");
		if("showCategoryByPage".equals(op)) {
			// 分页查询
			showCategoryByPage(request,response);
		} else if("readyEditCategoryById".equals(op)) {
			// 准备修改
			readyEditCategoryById(request,response);
		} else if("doEditCategoryById".equals(op)) {
			// 开始修改
			doEditCategoryById(request,response);
		} else if("removeCategoryById".equals(op)) {
			// 删除
			removeCategoryById(request,response);
		} else if("addCategory".equals(op)) {
			// 添加
			addCategory(request,response);
		} else if("checkCategoryNameByEdit".equals(op)) {
			// 修改 校验ajax
			checkCategoryNameByEdit(request,response);
		} else if("checkCategoryNameByAdd".equals(op)) {
			// 增加 校验ajax
			checkCategoryNameByAdd(request,response);
		} else if("doSearchCategory".equals(op)) {
			// ajax 条件查询数据
			doSearchCategory(request,response);
		}
		
		
		
	}

	/**
	 * ajax 条件查询数据
	 */
	protected void doSearchCategory(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		int offset = Integer.parseInt(request.getParameter("offset"));
		// offset [忽略数 : 从第几位后开始]
		int pageSize = Integer.parseInt(request.getParameter("limit"));
		int currentPage = (offset / pageSize) + 1;
		
		String categoryName = request.getParameter("cateName");
		
		Page<Categorys> page = cates.findCategorysListByCondition(new Page<Categorys>(currentPage, pageSize), new Categorys(categoryName));
		
		BootStrapPageHelper<Categorys> pageHelper = new BootStrapPageHelper<Categorys>();
		
		pageHelper.setTotal(page.getTotal());
		pageHelper.setRows(page.getList());
		
		Gson gson = new Gson();
		response.getWriter().print(gson.toJson(pageHelper));
		
	}

	/**
	 * 增加 校验ajax
	 */
	protected void checkCategoryNameByAdd(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		String cateName = request.getParameter("cateName");
		String str = null;
		boolean f = cates.findCategoryByName(cateName);
		if(f) {
			str = "可用";
		} else {
			str = "重名";
		}
		
		response.getWriter().print(str);
	}

	/**
	 * 修改 校验ajax
	 */
	protected void checkCategoryNameByEdit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int cateId = Integer.parseInt(request.getParameter("id"));
		String cateName = request.getParameter("cateName");
		String str = null;
		
		// id相同,名称不修改不构成重名
		Categorys categorys = cates.findCategoryByPName(cateName);
		
		// 根据名称查询 category
		boolean f = cates.findCategoryByName(cateName);
		if(f) {
			str = "可用";
		} else {
			if(categorys.getCategoryId() == cateId) {
				str = "可用";
			} else {
				str = "重名";
			}
		}
		
		response.getWriter().print(str);
	}

	/**
	 * 添加
	 */
	protected void addCategory(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		
		String categoryName = request.getParameter("cateName");
		String categoryDesc = request.getParameter("cateDesc");
		
		int i = cates.addCategorys(new Categorys(categoryName, categoryDesc));
		if(i == 302) {
			out.write("<script>alert('提示信息: 类别添加成功.');location.href='searchCategorys.jsp';</script>");
		} else if(i == 300) {
			out.write("<script>alert('提示信息: 类别添加失败,有空字符串.');location.href='addCategory.jsp';</script>");
		} else if(i == 301) {
			out.write("<script>alert('提示信息: 类别添加失败,该类别名称已存在.');location.href='addCategory.jsp';</script>");
		}
		
	}

	/**
	 * 删除
	 */
	protected void removeCategoryById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		int categoryId = Integer.parseInt(request.getParameter("id"));
		
		int i = cates.removeCategorys(categoryId);
		if(i == 302) {
			out.write("<script>alert('提示信息: 类别删除成功.');location.href='searchCategorys.jsp';</script>");
		} else if(i == 300) {
			out.write("<script>alert('提示信息: 类别删除失败,该类别下的商品不为空.');location.href='searchCategorys.jsp';</script>");
		} else if(i == 301) {
			out.write("<script>alert('提示信息: 类别删除失败,没有该类别.');location.href='searchCategorys.jsp';</script>");
		}
		
	}

	/**
	 * 开始修改
	 */
	protected void doEditCategoryById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		
		int categoryId = Integer.parseInt(request.getParameter("cateId"));
		String categoryName = request.getParameter("cateName");
		String categoryDesc = request.getParameter("cateDesc");
		
		int i = cates.modifyCategorys(new Categorys(categoryId, categoryName, categoryDesc));
		if(i == 302) {
			out.write("<script>alert('提示信息: 类别修改成功.');location.href='searchCategorys.jsp';</script>");
		} else if(i == 300) {
			out.write("<script>alert('提示信息: 类别修改失败,有空字符串.');location.href='searchCategorys.jsp';</script>");
		} else if(i == 301) {
			out.write("<script>alert('提示信息: 类别修改失败,该类别名称已存在.');location.href='searchCategorys.jsp';</script>");
		}
	}

	/**
	 * 准备修改
	 */
	protected void readyEditCategoryById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		int categoryId = Integer.parseInt(request.getParameter("id"));
		
		Categorys categorys = cates.findCategoryById(categoryId);
		request.setAttribute("cate", categorys);
		
		request.getRequestDispatcher("editCategoryById.jsp").forward(request, response);
	}

	/**
	 * 分页查询
	 */
	protected void showCategoryByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int currentPage = Integer.parseInt(request.getParameter("currentPage"));
		Page<Categorys> page = cates.findCategorysListByPage(new Page<Categorys>(currentPage, 5));
		
		request.setAttribute("page", page);
		request.getRequestDispatcher("showAllCategorys.jsp").forward(request, response);
	}

	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
